package com.yu.footballpromotionliveadmin.dao.impl;

import com.yu.footballpromotionliveadmin.dao.EventRecordsDao;
import com.yu.footballpromotionliveadmin.mapper.EventRecordsMapper;
import com.yu.footballpromotionliveadmin.page.PageInfo;
import com.yu.footballpromotionliveadmin.pojo.EventRecords;
import com.yu.footballpromotionliveadmin.util.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;


@Repository
public class EventRecordsDaoImpl implements EventRecordsDao {

    @Resource
    EventRecordsMapper recordsMapper;

    @Override
    public List<EventRecords> getAllCompetitionRecors() throws Exception {
        return recordsMapper.selectByExample(null);
    }

    @Override
    public EventRecords getCompetitionRecorByUno(String erno) throws Exception {
        return recordsMapper.selectByPrimaryKey(erno);
    }

    @Override
    public List<EventRecords> getCompetitionRecorsByPage(PageInfo pageInfo) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount =(int) recordsMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return recordsMapper.getEventRecordsByPage(map);
    }

    @Override
    public Integer deleteCompetitionRecor(String erno) throws Exception {
        return recordsMapper.deleteByKey(erno);
    }

    @Override
    public Integer updateCompetitionRecor(EventRecords eventRecords) throws Exception {
        return recordsMapper.updateByPrimaryKey(eventRecords);
    }

    @Override
    public Integer addCompetitionRecorByUno(EventRecords eventRecords) throws Exception {
        eventRecords.setErno(initId());
        return recordsMapper.insert(eventRecords);
    }

    @Override
    public EventRecords getCompetitionRecorsWithOrders(String erno) throws Exception {
        return null;
    }

    @Override
    public EventRecords getAccountByAccountName(String erno) throws Exception {
        return null;
    }

    public String initId() {
        String maxUno = recordsMapper.getMaxUno();
        maxUno =  Optional.ofNullable(maxUno).orElse("R0000");
        return GenNumTools.initId("R", 4, maxUno);
    }

}
